﻿/***************************************************************
 * 开发人员：网魂小兵@http://www.mozlite.com
 * 
 * 目标标本：4.0.30319.239
 * 
 * 最后修改：2011/12/19 2:07:21
 * ************************************************************/
namespace Mozlite.Web.UI
{
    using System;
    using System.Linq;
    using System.Web;
    using System.Web.Mvc;
    using System.Web.Routing;
    using System.Web.Security;
    using Mozlite.Common;
    using System.Collections.Generic;
    using Mozlite.Common.Configuration;

    /// <summary>
    /// 应用程序全局类型。
    /// </summary>
    public class MvcApplication : System.Web.HttpApplication
    {
        public static void RegisterGlobalFilters(GlobalFilterCollection filters)
        {
            filters.Add(new HandleErrorAttribute());
        }

        protected void Application_Start()
        {
            Logs.Info("应用程序启动：\r\n\r\n重新注册URL地址，启动后台服务等等初始化服务！", "Web 事件", EventID.Events, "ASP.NET " + Globals.DotNetVersion.ToString(), SiteSettings.Default.SettingsID);
            ViewEngines.Engines.Clear();
            ViewEngines.Engines.Add(new ThemeViewEngine());
            AreaRegistration.RegisterAllAreas();

            RegisterGlobalFilters(GlobalFilters.Filters);
            RouteManager.RegisterRoutes(RouteTable.Routes);
            //启动后台任务
            Tasks.Start();
        }

        protected void Application_BeginRequest()
        {
            AppContext.Create(Context);
            Globals.ValidUrl();
        }

        /// <summary>
        /// 应用程序请求开始执行方法。
        /// </summary>
        /// <param name="sender">应用程序实例对象。</param>
        /// <param name="e">事件实例对象。</param>
        protected void Application_AuthorizeRequest(object sender, EventArgs e)
        {
            //if (!this.IsInstaller(Context))
            //{
            AppContext current = AppContext.Current;
            if (current.User == null)
            {
                FormsAuthentication.SignOut();
                current.User = Users.GetAnonymousUser(true);
                Response.Redirect(Globals.ApplicationUrl);
            }
            //}
        }

        protected void Application_End(object sender, EventArgs e)
        {
            Globals.RecordEndReason();
        }
    }
}